package com.uzai.console.service.tiktok.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.KefuSendMsgEnum;
import com.uzai.common.enums.MessageFuncEnum;
import com.uzai.common.enums.MessageTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.msg.MsgDto;
import com.uzai.common.msg.NoticeMsgDto;
import com.uzai.common.result.ResponseCode;
import com.uzai.common.utils.EsIndexName;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.CacheKey;
import com.uzai.console.constant.ES.ESResult;
import com.uzai.console.constant.ES.ElasticsearchUtilGroupMsg;
import com.uzai.console.dto.feign.titok.TiktokGetConversDto;
import com.uzai.console.dto.rocketmq.wxgroup.GetGroupMemberListDto;
import com.uzai.console.dto.rocketmq.wxgroup.QuitGroupDto;
import com.uzai.console.dto.tiktok.group.tiktokgroup.*;
import com.uzai.console.dto.wechatprivate.wxq.manageplan.WxqGroupManagePlanQuery;
import com.uzai.console.entity.*;
import com.uzai.console.entity.es.EsTiktokConversation;
import com.uzai.console.entity.es.EsWxContacts;
import com.uzai.console.enums.DeviceProductEnum;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.feign.UzaiImClientFeignService;
import com.uzai.console.service.feign.WxContactsFeignService;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.service.rocketmq.NoticeMsgPushProducerService;
import com.uzai.console.service.tiktok.TiktokConversationService;
import com.uzai.console.vo.tiktok.group.tiktokgorup.TiktokConversationVo;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.CardinalityAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 个人抖音-抖音群
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class TiktokConversationServiceImpl implements TiktokConversationService {

    Logger logger = LoggerFactory.getLogger(TiktokConversationServiceImpl.class);

    @Autowired
    private ElasticsearchUtilGroupMsg elasticsearchUtilGroupMsg;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private NoticeMsgPushProducerService noticeMsgPushProducerService;
    @Autowired
    private WxContactsFeignService wxContactsFeignService;
    @Autowired
    private DeviceTiktokInfoMapper deviceTiktokInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WxqGroupManagePlanMapper wxqGroupManagePlanMapper;
    @Autowired
    private WxqManagePlanMapper wxqManagePlanMapper;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private SysPluginMapper sysPluginMapper;
    @Autowired
    private DeviceAuthMapper deviceAuthMapper;
    @Autowired
    private UzaiImClientFeignService uzaiImClientFeignService;

    /**
     * 查询抖音群列表
     *
     * @param tiktokConversationQuery
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public Page<TiktokConversationVo> findTiktokConversationList(TiktokConversationQuery tiktokConversationQuery) {
        //新建返回到页面的LIST对象
        List<TiktokConversationVo> tiktokConversationVoList = new ArrayList<>();

        //执行分页查询对象
        Page<TiktokConversationVo> page = new Page<>();
        tiktokConversationQuery.convert(page);

        //ES查询
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQuery_and = QueryBuilders.boolQuery();
        BoolQueryBuilder boolQuery_or = QueryBuilders.boolQuery();
        BoolQueryBuilder boolQuery_or_deviceList = QueryBuilders.boolQuery();
        BoolQueryBuilder boolQuery_or_keyword = QueryBuilders.boolQuery();
        //BoolQueryBuilder boolQuery_or_groupIdExcludeList = QueryBuilders.boolQuery();
        //BoolQueryBuilder boolQuery_or_wxidExcludeList = QueryBuilders.boolQuery();

        //运营商ID
        boolQuery_and.must(QueryBuilders.termQuery("merId", Tools.getLong(tiktokConversationQuery.getMerId())));

        //类型 1单聊 2群聊
        boolQuery_and.must(QueryBuilders.termQuery("type", 2));

        //会话id
        if(StringUtils.isNotBlank(tiktokConversationQuery.getConversationId())){
            boolQuery_and.must(QueryBuilders.termQuery("conversationId", tiktokConversationQuery.getConversationId()));
        }

        //名称
        if(StringUtils.isNotBlank(tiktokConversationQuery.getName())){
            boolQuery_and.must(QueryBuilders.matchQuery("name",tiktokConversationQuery.getName()));
        }

        //机器人唯一ID
        if(tiktokConversationQuery.getDeviceUniqueId() != null){
            boolQuery_and.must(QueryBuilders.termQuery("deviceUniqueId", tiktokConversationQuery.getDeviceUniqueId()));
        }

        //管理方案id
        if(tiktokConversationQuery.getManagePlanId() != null){
            boolQuery_and.must(QueryBuilders.termQuery("managePlanId", tiktokConversationQuery.getManagePlanId()));
        }

        //开启过滤未配置管理方案的群
        if(Tools.getInteger(tiktokConversationQuery.getManagePlanIdFlag()).intValue() == 1){
            boolQuery_and.must(QueryBuilders.rangeQuery("managePlanId").gte(0));
        }

        //排除抖音群id(id主键)
        List<String> idExcludeList = tiktokConversationQuery.getIdExcludeList();
        if(idExcludeList != null && idExcludeList.size() > 0){
            for (String id : idExcludeList) {
                boolQuery_and.mustNot(QueryBuilders.termQuery("id", id));
            }
        }

        //排除群会话id
        List<String> conversationIdExcludeList = tiktokConversationQuery.getConversationIdExcludeList();
        if(conversationIdExcludeList != null && conversationIdExcludeList.size() > 0){
            for (String conversationId : conversationIdExcludeList) {
                boolQuery_and.mustNot(QueryBuilders.termQuery("conversationId", conversationId));
            }
        }

        //机器人唯一id列表
        List<Long> deviceUniqueIdList = tiktokConversationQuery.getDeviceUniqueIdList();
        if(deviceUniqueIdList != null && deviceUniqueIdList.size() > 0){
            for (Long deviceUniqueId : deviceUniqueIdList) {
                boolQuery_or_deviceList.should(QueryBuilders.termQuery("deviceUniqueId", deviceUniqueId));
            }
        }

        //子账号绑定机器人
        List<Long> subDeviceUniqueIdList = tiktokConversationQuery.getSubDeviceUniqueIdList();
        if(subDeviceUniqueIdList != null && subDeviceUniqueIdList.size() > 0){
            for (Long deviceUniqueId : subDeviceUniqueIdList) {
                boolQuery_or.should(QueryBuilders.termQuery("deviceUniqueId", deviceUniqueId));
            }
        }

        //关键词搜索-群昵称
        if(StringUtils.isNotBlank(tiktokConversationQuery.getKeyword())){
            boolQuery_or_keyword.should(QueryBuilders.matchQuery("name", tiktokConversationQuery.getKeyword()));
            boolQuery_or_keyword.should(QueryBuilders.termQuery("deviceUniqueId.keyword", tiktokConversationQuery.getKeyword()));
        }

        searchSourceBuilder.query(boolQuery_and.must(boolQuery_or).must(boolQuery_or_deviceList).must(boolQuery_or_keyword));

        //searchSourceBuilder.query(boolQuery_and.must(boolQuery_or).must(boolQuery_or_deviceList).mustNot(boolQuery_or_groupIdExcludeList).mustNot(boolQuery_or_wxidExcludeList));

        //判断根据群wxid去重
        //去重计数字段
        String distinctCount = "distinctCount";
        if(Tools.getInteger(tiktokConversationQuery.getConversationIdDistinctFlag()).intValue() == 1){//去重
            //需要去重的字段名
            CollapseBuilder collapseBuilder = new CollapseBuilder("conversationId");//针对群conversationId字段去重
            //查询去重后的结果数量计数（前者参数为计数自定义名字，后者参数为指定需要计数的字段）
            CardinalityAggregationBuilder aggregationBuilder = AggregationBuilders.cardinality(distinctCount).field("conversationId");
            searchSourceBuilder.collapse(collapseBuilder);
            searchSourceBuilder.aggregation(aggregationBuilder);
        }

        //logger.info(searchSourceBuilder.toString());

        String esName = EsIndexName.UZAI_TIKTOK_CONVERSATION;
        String mechantOtherEsIndexName = merchantService.getMechantOtherEsIndexName(esName, tiktokConversationQuery.getMerId());

        ESResult esResult = elasticsearchUtilGroupMsg.distinctSearchListData(mechantOtherEsIndexName, searchSourceBuilder, tiktokConversationQuery.getSize(), tiktokConversationQuery.getFromIndex(), null, null, null, tiktokConversationQuery.getMerId(),distinctCount);
        page.setTotal(esResult.getTotalHits());
        List<Map<String, Object>> dataList = esResult.getDataList();
        if(dataList != null && dataList.size() > 0){
            for (Map<String, Object> dataMp :dataList) {
                TiktokConversationVo tiktokConversationVo  = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), TiktokConversationVo.class);
                //机器人信息
                DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectById(tiktokConversationVo.getDeviceUniqueId(), tiktokConversationVo.getMerId());
                if(deviceTiktokInfo != null){
                    tiktokConversationVo.setDeviceName(deviceTiktokInfo.getDeviceName());
                    tiktokConversationVo.setDeviceRemark(deviceTiktokInfo.getDeviceRemark());
                }

                //管理方案信息
                Long managePlanId = tiktokConversationVo.getManagePlanId();
                if(managePlanId != null && managePlanId != 0){
                    ConfigPlan configPlan = configPlanMapper.selectById(managePlanId, tiktokConversationVo.getMerId());
                    if(configPlan != null){
                        tiktokConversationVo.setManagePlanId(managePlanId);
                        tiktokConversationVo.setManagePlanName(configPlan.getName());
                        tiktokConversationVo.setManageConfigFlag(1); //配置了管理方案
                        //查询配置方案的功能设置
                        if(configPlan.getGroupBasesetId() != null){
                            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getGroupBasesetId(), configPlan.getMerId());
                            if(sysPlugin != null){
                                JSONObject jsonObject = JSONObject.parseObject(sysPlugin.getData());
                                if(jsonObject != null){
                                    //是否开启返利功能
                                    tiktokConversationVo.setFlSwitch(Tools.getInteger(jsonObject.getInteger("flSwitch")));
                                    //是否开启免单功能
                                    tiktokConversationVo.setMdSwitch(Tools.getInteger(jsonObject.getInteger("mdSwitch")));
                                    //是否开启发单功能
                                    tiktokConversationVo.setFdSwitch(Tools.getInteger(jsonObject.getInteger("fdSwitch")));
                                    //是否开启群内提现功能
                                    tiktokConversationVo.setTxSwitch(Tools.getInteger(jsonObject.getInteger("txSwitch")));
                                }
                            }
                        }
                    }
                }

                //判断是否购买了发单功能
                int now = DateUtil.getNowTime();
                DeviceAuth deviceAuth_fd = deviceAuthMapper.selectByDeviceIdAndProductCode(tiktokConversationVo.getDeviceId(), DeviceProductEnum.FD.getValue());
                if(deviceAuth_fd != null){
                    if(deviceAuth_fd.getExpireTime() > now){
                        tiktokConversationVo.setFdBuyFlag(1);
                    }
                }

                //怕段是否购买了托功能
                DeviceAuth deviceAuth_tuo = deviceAuthMapper.selectByDeviceIdAndProductCode(tiktokConversationVo.getDeviceId(), DeviceProductEnum.TUO.getValue());
                if(deviceAuth_tuo != null){
                    if(deviceAuth_tuo.getExpireTime() > now){
                        tiktokConversationVo.setTuoBuyFlag(1);
                    }
                }

                tiktokConversationVoList.add(tiktokConversationVo);
            }
        }

        //返回到页面分页对象
        page.setRecords(tiktokConversationVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return page;
    }

    /**
     * 查询抖音群详情
     *
     * @param tiktokConversationIdDto
     * @return null
     * @author liuqi
     * @date 2021/8/13 11:38
     */
    public EsTiktokConversation findTiktokConversationInfo(TiktokConversationIdDto tiktokConversationIdDto){
        EsTiktokConversation esTiktokConversation = null;

        if(tiktokConversationIdDto.getId() == null || tiktokConversationIdDto.getMerId() == null){
            return esTiktokConversation;
        }

        try{
            String esName = EsIndexName.UZAI_TIKTOK_CONVERSATION;
            String mechantOtherEsIndexName = merchantService.getMechantOtherEsIndexName(esName, tiktokConversationIdDto.getMerId());
            Map<String, Object> dataMp = elasticsearchUtilGroupMsg.searchDataById(mechantOtherEsIndexName, tiktokConversationIdDto.getId(), null);
            esTiktokConversation  = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), EsTiktokConversation.class);
        }catch (Exception e){
            logger.error("--查询抖音微信群详情失败--,error={}", e.getMessage());
        }

        return esTiktokConversation;
    }

    /**
     * 单个发送消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tiktokConversationSendMessageDto
     * @return null
     */
    public void sendMessage(TiktokConversationSendMessageDto tiktokConversationSendMessageDto){

        if(StringUtils.isBlank(tiktokConversationSendMessageDto.getId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入主键id");
        }

        if(StringUtils.isBlank(tiktokConversationSendMessageDto.getMessage())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入消息内容");
        }

        //查询抖音群
        String esName = EsIndexName.UZAI_TIKTOK_CONVERSATION;
        String mechantOtherEsIndexName = merchantService.getMechantOtherEsIndexName(esName, tiktokConversationSendMessageDto.getMerId());

        Map<String, Object> dataMp = elasticsearchUtilGroupMsg.searchDataById(mechantOtherEsIndexName, tiktokConversationSendMessageDto.getId(), null);
        TiktokConversationVo tiktokConversationVo = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), TiktokConversationVo.class);
        if (tiktokConversationVo != null) {
            DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectById(tiktokConversationVo.getDeviceUniqueId(), tiktokConversationSendMessageDto.getMerId());
            if(deviceTiktokInfo != null){
                //发送MQ消息
                MsgDto<NoticeMsgDto>  msgDto = new MsgDto<>();
                NoticeMsgDto  noticeMsgDto = new NoticeMsgDto();
                noticeMsgDto.setBizUserId(tiktokConversationVo.getConversationId());
                noticeMsgDto.setContent(tiktokConversationSendMessageDto.getMessage());
                noticeMsgDto.setContentType(MessageTypeEnum.MT_TEXT.getValue());
                noticeMsgDto.setGroupId(null);
                noticeMsgDto.setNickName(tiktokConversationVo.getName());
                noticeMsgDto.setMsgSource(1);
                noticeMsgDto.setMsgBizType(KefuSendMsgEnum.UNSOLICITEDMSG.getKey());
                msgDto.setContent(noticeMsgDto);
                msgDto.setId(IdWorker.getId());
                msgDto.setDeviceType(deviceTiktokInfo.getRecvmsgType());
                msgDto.setDeviceUniqueId(tiktokConversationVo.getDeviceUniqueId());
                msgDto.setMerId(tiktokConversationSendMessageDto.getMerId());
                msgDto.setMsgType(MessageFuncEnum.SEND_CHAT_MESSAGE.getValue());
                noticeMsgPushProducerService.sendMessage(msgDto);
            }
        }
    }

    /**
     * 批量发送消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tiktokConversationSendMessageBatchDto
     * @return null
     */
    public void batchSendMessage(TiktokConversationSendMessageBatchDto tiktokConversationSendMessageBatchDto){
        //抖音群id列表
        List<String> idList = tiktokConversationSendMessageBatchDto.getIdList();
        //发送消息
        String message = tiktokConversationSendMessageBatchDto.getMessage();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择批量操作的抖音群");
        }
        if(StringUtils.isBlank(message)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入消息内容");
        }

        for(String id : idList){
            try {
                TiktokConversationSendMessageDto tiktokConversationSendMessageDto = new TiktokConversationSendMessageDto();
                BeanUtils.copyProperties(tiktokConversationSendMessageBatchDto, tiktokConversationSendMessageDto);
                tiktokConversationSendMessageDto.setId(id);
                tiktokConversationSendMessageDto.setMerId(tiktokConversationSendMessageBatchDto.getMerId());
                tiktokConversationSendMessageDto.setMessage(message);
                sendMessage(tiktokConversationSendMessageDto);
            }catch (Exception e){

            }
        }
    }


    /**
     * 修改管理方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tiktokConversationUpdateManagePlanDto
     * @return null
     */
    public void updateManagePlan(TiktokConversationUpdateManagePlanDto tiktokConversationUpdateManagePlanDto){
        if(StringUtils.isBlank(tiktokConversationUpdateManagePlanDto.getId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入主键id");
        }
        //查询抖音群
        String esName = EsIndexName.UZAI_TIKTOK_CONVERSATION;
        String mechantOtherEsIndexName = merchantService.getMechantOtherEsIndexName(esName, tiktokConversationUpdateManagePlanDto.getMerId());
        Map<String,Object> dataMp = elasticsearchUtilGroupMsg.searchDataById(mechantOtherEsIndexName, tiktokConversationUpdateManagePlanDto.getId(), null);
        EsTiktokConversation esTiktokConversation = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), EsTiktokConversation.class);
        if(esTiktokConversation == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该抖音群不存在");
        }
        int now = DateUtil.getNowTime();
        //缓存中的抖音群管理方案key
        String redisKey_tiktokGroup_manageplan = String.format(RedisCacheKeyEnum.TIKTOK_GROUP_MANAGEPLAN_CONFIG.getKey(), esTiktokConversation.getMerId(), esTiktokConversation.getDeviceId(), esTiktokConversation.getConversationId());
        //如果管理方案为空，则表示清空管理方案
        if(tiktokConversationUpdateManagePlanDto.getManagePlanId() == null){ //清除管理方案
            //清除ES
            esTiktokConversation.setManagePlanId(null);
            esTiktokConversation.setUpdateTime(now);
            elasticsearchUtilGroupMsg.addData(esTiktokConversation, mechantOtherEsIndexName, esTiktokConversation.getId());
            //清除缓存
            redisTemplate.delete(redisKey_tiktokGroup_manageplan);
        }else{//修改管理方案
            esTiktokConversation.setManagePlanId(tiktokConversationUpdateManagePlanDto.getManagePlanId());
            esTiktokConversation.setUpdateTime(now);
            elasticsearchUtilGroupMsg.addData(esTiktokConversation, mechantOtherEsIndexName, esTiktokConversation.getId());
            //修改缓存
            redisTemplate.opsForValue().set(redisKey_tiktokGroup_manageplan, tiktokConversationUpdateManagePlanDto.getManagePlanId());
        }
    }

    /**
     * 批量修改管理方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tiktokConversationBatchUpdateManagePlanDto
     * @return null
     */
    public void batchUpdateManagePlan(TiktokConversationBatchUpdateManagePlanDto tiktokConversationBatchUpdateManagePlanDto){
        //抖音群id列表
        List<String> idList = tiktokConversationBatchUpdateManagePlanDto.getIdList();
        //管理方案
        Long managePlanId = tiktokConversationBatchUpdateManagePlanDto.getManagePlanId();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择批量操作的抖音群");
        }
        for(String id  : idList){
            try {
                TiktokConversationUpdateManagePlanDto tiktokConversationUpdateManagePlanDto = new TiktokConversationUpdateManagePlanDto();
                BeanUtils.copyProperties(tiktokConversationBatchUpdateManagePlanDto, tiktokConversationUpdateManagePlanDto);
                tiktokConversationUpdateManagePlanDto.setMerId(tiktokConversationBatchUpdateManagePlanDto.getMerId());
                tiktokConversationUpdateManagePlanDto.setId(id);
                tiktokConversationUpdateManagePlanDto.setManagePlanId(managePlanId);
                updateManagePlan(tiktokConversationUpdateManagePlanDto);
            }catch (Exception e){

            }
        }
    }

    /**
     * 刷新抖音群
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tiktokConversationRefreshDto
     * @return null
     */
    public void refreshTiktokConversation(TiktokConversationRefreshDto tiktokConversationRefreshDto){
        if(tiktokConversationRefreshDto.getDeviceUniqueId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择机器人");
        }

        DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectById(tiktokConversationRefreshDto.getDeviceUniqueId(), tiktokConversationRefreshDto.getMerId());

        if(deviceTiktokInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "抖音机器人不存在");
        }

        //测试服方便测试，抛弃5分钟限流
        if(tiktokConversationRefreshDto.getMerId() != 1547482020152213506L){
            String val = (String) redisTemplate.opsForValue().get(String.format(CacheKey.TIKTOK_CONVASATION_REFRESH, tiktokConversationRefreshDto.getMerId(), tiktokConversationRefreshDto.getDeviceUniqueId()));
            if (StringUtils.isNotBlank(val)){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "5分钟只允许同一个机器人请求一次");
            }
        }

        //往reids插入本次下载，允许5分钟请求一次，避免频繁提交
        redisTemplate.opsForValue().set(String.format(CacheKey.TIKTOK_CONVASATION_REFRESH, tiktokConversationRefreshDto.getMerId(), tiktokConversationRefreshDto.getDeviceUniqueId()),tiktokConversationRefreshDto.getDeviceUniqueId()+"",5, TimeUnit.MINUTES);

        //发送MQ消息
        MsgDto<TiktokGetConversDto> msgDto = new MsgDto<>();
        msgDto.setContent(null);
        msgDto.setId(IdWorker.getId());
        msgDto.setDeviceType(deviceTiktokInfo.getRecvmsgType());
        msgDto.setDeviceUniqueId(deviceTiktokInfo.getId());
        msgDto.setMerId(deviceTiktokInfo.getMerId());
        msgDto.setMsgType(MessageFuncEnum.TIKTOK_GET_CONVERS.getValue());
        TiktokGetConversDto tiktokGetConversDto = new TiktokGetConversDto();
        tiktokGetConversDto.setImUid(deviceTiktokInfo.getImUid());
        tiktokGetConversDto.setStartTime(System.currentTimeMillis());
        msgDto.setContent(tiktokGetConversDto);
        UzaiRespVo<Object> uzaiRespVo = uzaiImClientFeignService.reflashTiktokConversation(msgDto);
        logger.info("--调用刷新抖音群会话接口--para={},result={}",JSONObject.toJSONString(msgDto), JSONObject.toJSONString(uzaiRespVo));

        if(uzaiRespVo != null && uzaiRespVo.getCode() != 200){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, uzaiRespVo.getMsg());
        }

    }

}
